﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUDataAcquisition.DataAccess;
using RTUDataAcquisition.AppCode;

namespace RTUDataAcquisition
{
    public partial class StationConfig : Form
    {
        public StationConfig()
        {
            InitializeComponent();
        }

        private void InitStationConfig()
        {
            try
            {
                string strSqlCondition = string.Empty;
                if (txtSelect.Text.Trim() != string.Empty)
                {
                    if (cbxSelect1.SelectedIndex == 0)//编号
                    {
                        strSqlCondition = " where StationNo like '%" + txtSelect.Text.Trim() + "%'";
                    }
                    else //名称
                    {
                        strSqlCondition = " where StationName like '%" + txtSelect.Text.Trim() + "%'";
                    }
                }
                DataTable dt = DataBusiness.GetStationConfig(strSqlCondition);
                dgvInfo.DataSource = dt;
                toolStripStatusLabel2.Text = dgvInfo.Rows.Count.ToString();
                if (dgvInfo.SelectedRows.Count > 0)
                {
                    dgvInfo.SelectedRows[0].Selected = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                //if (txtSelect.Text.Trim() == string.Empty)
                //{
                //    Messages.ShowMessage("请输入编号或名称!");
                //    return;
                //}

                InitStationConfig();
                if (dgvInfo.Rows.Count == 0)
                {
                    Messages.ShowMessage("搜索无相关站点数据!");
                }
            }
            catch (Exception ex)
            {
                Messages.SaveLog(ex, ELogLayer.UI, ELogType.Info);
            }
        }

        private void StationConfig_Load(object sender, EventArgs e)
        {
            try
            {
                cbxSelect1.SelectedIndex = 0;
                InitStationConfig();
            }
            catch (Exception ex)
            {
                Messages.SaveLog(ex, ELogLayer.UI, ELogType.Info);
            }
        }

        /// <summary>
        /// 修改站点
        /// </summary>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvInfo.SelectedRows.Count == 0)
                {
                    Messages.ShowMessage("请选择要修改的站点!");
                    return;
                }

                if (DataBusiness.UpdateStationConfig(dgvInfo.SelectedRows[0].Cells[0].Value.ToString(), dgvInfo.SelectedRows[0].Cells[1].Value.ToString(), Convert.ToInt32(dgvInfo.SelectedRows[0].Cells[2].Value),
                    Convert.ToInt32(dgvInfo.SelectedRows[0].Cells[3].Value), Convert.ToInt32(dgvInfo.SelectedRows[0].Cells[4].Value)))
                {
                    Messages.ShowMessage("修改站点成功!");
                }
                else
                {
                    Messages.ShowMessage("修改站点失败!");
                }
            }
            catch (Exception ex)
            {
                Messages.ShowMessage("修改站点失败!");
                Messages.SaveLog(ex, ELogLayer.UI, ELogType.Info);
            }
        }

        /// <summary>
        /// 删除站点
        /// </summary>
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvInfo.SelectedRows.Count == 0)
                {
                    Messages.ShowMessage("请选择要删除的站点!");
                    return;
                }

                string strStationNo = dgvInfo.SelectedRows[0].Cells[0].Value.ToString();
                if (MessageBox.Show(string.Format("确定要删除站点 {0} ?\r\n删除后将不可恢复!", strStationNo), this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    if (DataBusiness.DeleteStationConfig(strStationNo))
                    {
                        InitStationConfig();
                    }
                    else
                    {
                        Messages.ShowMessage("删除站点失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                Messages.ShowMessage("删除站点失败!");
                Messages.SaveLog(ex, ELogLayer.UI, ELogType.Info);
            }
        }

        public DataGridViewTextBoxEditingControl cellEdit = null;

        private void dgvInfo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dgvInfo.CurrentCellAddress.X == 2 || this.dgvInfo.CurrentCellAddress.X == 3 || this.dgvInfo.CurrentCellAddress.X == 4)//获取当前处于活动状态的单元格索引
            {
                cellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                cellEdit.SelectAll();
                cellEdit.KeyPress += Cells_KeyPress; //绑定事件
            }
        }

        private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
        {
            if (this.dgvInfo.CurrentCellAddress.X == 2 || this.dgvInfo.CurrentCellAddress.X == 3 || this.dgvInfo.CurrentCellAddress.X == 4) //获取当前处于活动状态的单元格索引
            {
                if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
                if (e.KeyChar == '\b') e.Handled = false;
            }
        }
    }
}
